package business_opportunity

import (
	"git.mycaigou.com/gfyx/micro-gfyx-api/http_server/controller"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/common/errcode"
	dto "git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/dto/business_opportunity"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/utils"
	projectService "git.mycaigou.com/gfyx/micro-gfyx-api/service/business_opportunity/project"
	"git.myscrm.cn/golang/stark/v4"
	"github.com/gin-gonic/gin"
)

type LandController struct {
	controller.BaseController
	landService projectService.LandServiceIface
}

// NewLandController 构造函数
func NewLandController(
	landService projectService.LandServiceIface,
) *LandController {
	return &LandController{
		landService: landService,
	}
}

// RegisterRouter
// 注册路由
func (c *LandController) RegisterRouter(g *gin.RouterGroup) {
	// 拿地列表接口
	g.POST("land/list", c.list)
	// 拿地列表导出
	g.POST("land/export-list", c.exportList)
}

func (c *LandController) list(ctx *gin.Context) {
	req := &dto.GetTakenLandRequest{}
	if err := ctx.ShouldBind(&req); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(req, err))
		return
	}

	res, err := c.landService.GetTakenLand(ctx, req)
	if err != nil {
		stark.Logger.Errorf(ctx, "land/list GetTakenLand request:%#v err:%s", req, err.Error())
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, err.Error())
		return
	}
	c.Success(ctx, res)
}

func (c *LandController) exportList(ctx *gin.Context) {
	req := &dto.GetTakenLandRequest{}
	if err := ctx.ShouldBind(&req); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(req, err))
		return
	}

	res, err := c.landService.ExportTakenLand(ctx, req)
	if err != nil {
		stark.Logger.Errorf(ctx, "land/export-list ExportTakenLand request:%#v err:%s", req, err.Error())
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, err.Error())
		return
	}
	c.Success(ctx, res)
}
